package com.livepan.server.utils;

import com.alibaba.fastjson.JSON;
import com.tencentcloudapi.cdn.v20180606.CdnClient;
import com.tencentcloudapi.cdn.v20180606.models.PurgeUrlsCacheRequest;
import com.tencentcloudapi.cdn.v20180606.models.PurgeUrlsCacheResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.livepan.server.error.BusinessException;
import com.livepan.server.error.EmBusinessError;

public class CDNUtil {

    public static synchronized void refreshUrl(String... url) throws BusinessException {
        try {

            Credential cred = new Credential(ConstUtil.secretId, ConstUtil.secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("cdn.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            CdnClient client = new CdnClient(cred, "ap-guangzhou", clientProfile);

            String params = "{\"Urls\":" + JSON.toJSONString(url) + "}";
            System.out.println(params);
            PurgeUrlsCacheRequest req = PurgeUrlsCacheRequest.fromJsonString(params, PurgeUrlsCacheRequest.class);

            PurgeUrlsCacheResponse resp = client.PurgeUrlsCache(req);

            System.out.println(PurgeUrlsCacheRequest.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
            throw new BusinessException(EmBusinessError.USER_AVATAR_URL_REFRESH_ERROR);
        }

    }


}
